#!/usr/X11R6/bin/python3.1
import cgi
import cgitb
import os
import sys
from pygallib import usersession
from pygallib import layoutlib
from pygallib import albumlib
from pygallib import userlib
from pygallib import picturelib

# Enable debug print
cgitb.enable()

# Printing Headers
logged_in = usersession.logged_in()

headers = {}
headers["Content-type"] = "text/html"
layoutlib.print_http_headers(headers)
layoutlib.print_page_open("PyGallery - Home")
print("<body>")
menu = []
menu.append(("Home", "gallery.cgi"))
if logged_in:
  menu.append(("Logout", "logout.cgi"))
  menu.append(("Account Settings", "account_settings.cgi"))
else:
  menu.append(("Login", "login.cgi"))
layoutlib.print_page_menu(menu)

# Get the fields from url
#fields = cgi.FieldStorage(open(sys.stdin.fileno(), 'rb'))
fields = cgi.FieldStorage()

# Get the id of the album and user
if "id" in fields:
  album_id = int(fields["id"].value)
  # Get album information
  album = albumlib.get_album(album_id)
  # Get user who created album
  user = userlib.get_user_by_id(album["user_id"])

# If they uploaded an image, there will be a picture tag
if "picture" in fields:
  picture = fields["picture"]
  if picture.file:
    # Save normal size
    f = open(os.environ["DOCUMENT_ROOT"]
        + "/PictureShare/users/"
        + user["username"] + "/"
        + "album"+ str(album["album_id"]) + "/"
        + picture.filename , "w")
    f.write(picture.file.read())    
    f.close()
    # Save thumbnail
    f = open(os.environ["DOCUMENT_ROOT"]
        + "/PictureShare/users/"
        + user["username"] + "/"
        + "albumthumb"+ str(album["album_id"]) + "/"
        + picture.filename , "w")
    picture.file.seek(0)
    f.write(picture.file.read())
    f.close()
    # Insert into DB
    label = picture.filename
    if(len(str(fields["label"].value)) != 0):
        label = fields["label"].value
    picturelib.add_picture(album["album_id"], user["user_id"], picture.filename, label)
    print("Upload Successful! ")
    # They are going to upload, printing upload form
    print("""Upload an image to album: <a href="album.cgi?id=%d">%s</a>""" 
        .format(album_id, album["name"]))
    print("""
    <form action="album_picture_add.cgi" 
      method="post" enctype="multipart/form-data">
      <ul>
        <input type="hidden" name="id" value="%d" />
        <li>
          <input name="picture" type="file" />
        </li>
        <li>
          <label for="label">Caption:</label> 
          <input type="text" maxlength="512" name="label" size="30"/>
        </li>
        <li>
          <input type="submit" value="Submit" />
        </li>
      </ul>
    </form>
    """.format(album_id))
else:
  # They are going to upload, printing upload form
  print("""Upload an image to album: <a href="album.cgi?id=%d">%s</a>""" % (album_id, album["name"]))
  print("""
  <form action="album_picture_add.cgi" method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="%d" /><br />
    <input name="picture" type="file" /><br />
    Caption: 
    <input type="text" maxlength=512 name="label" size="30"/><br />
    <input type="submit" value="Submit" />
  </form>
  """ % album_id)
  












